/**
 * Created by eatong on 17/2/28.
 */

import {AsyncStorage} from 'react-native'

export function initialData(callback) {
  AsyncStorage.getItem('total', (error, total) => {
    if (!total) {
      const totalList = [
        {
          "date": "2017-04-05",
          "recordDate": "2017-04-05",
          "time": 1,
          "type": "add"
        },
        {
          "date": "2017-04-09",
          "recordDate": "2017-04-09",
          "time": 4.5,
          "type": "add"
        },
        {
          "date": "2017-04-12",
          "recordDate": "2017-04-12",
          "time": 0.5,
          "type": "add"
        },
        {
          "date": "2017-04-14",
          "recordDate": "2017-04-14",
          "time": 1,
          "type": "add"
        },
        {
          "date": "2017-04-20",
          "recordDate": "2017-04-21",
          "time": 1.5,
          "type": "add"
        },
        {
          "date": "2017-04-21",
          "recordDate": "2017-04-21",
          "time": 2.5,
          "type": "add"
        },
        {
          "date": "2017-04-24",
          "recordDate": "2017-04-24",
          "time": 2,
          "type": "minus"
        },
        {
          "date": "2017-04-24",
          "recordDate": "2017-04-24",
          "time": 1,
          "type": "add"
        },
        {
          "date": "2017-04-25",
          "recordDate": "2017-04-25",
          "time": 0.5,
          "type": "minus"
        },
        {
          "date": "2017-04-25",
          "recordDate": "2017-04-25",
          "time": 4,
          "type": "minus"
        },
        {
          "date": "2017-04-27",
          "recordDate": "2017-04-28",
          "time": 1.5,
          "type": "add"
        },
        {
          "date": "2017-04-28",
          "recordDate": "2017-04-28",
          "time": 2,
          "type": "add"
        },
        {
          "date": "2017-05-02",
          "recordDate": "2017-05-02",
          "time": 1,
          "type": "add"
        },
        {
          "date": "2017-05-03",
          "recordDate": "2017-05-04",
          "time": 1,
          "type": "add"
        },
        {
          "date": "2017-05-08",
          "recordDate": "2017-05-09",
          "time": 1,
          "type": "add"
        },
        {
          "date": "2017-05-09",
          "recordDate": "2017-05-10",
          "time": 1,
          "type": "add"
        },
        {
          "date": "2017-05-10",
          "recordDate": "2017-05-10",
          "time": 1.5,
          "type": "add"
        },
        {
          "date": "2017-05-12",
          "recordDate": "2017-05-12",
          "time": 4.5,
          "type": "add"
        },
        {
          "date": "2017-05-11",
          "recordDate": "2017-05-16",
          "time": 1,
          "type": "add"
        },
        {
          "date": "2017-05-16",
          "recordDate": "2017-05-16",
          "time": 1.5,
          "type": "add"
        },
        {
          "date": "2017-05-20",
          "recordDate": "2017-05-23",
          "time": 7.5,
          "type": "add"
        },
        {
          "date": "2017-05-22",
          "recordDate": "2017-05-23",
          "time": 1,
          "type": "add"
        },
        {
          "date": "2017-05-23",
          "recordDate": "2017-05-23",
          "time": 2,
          "type": "add"
        },
        {
          "date": "2017-05-24",
          "recordDate": "2017-05-25",
          "time": 2,
          "type": "add"
        },
        {
          "date": "2017-05-25",
          "recordDate": "2017-05-25",
          "time": 3.5,
          "type": "add"
        },
        {
          "date": "2017-05-26",
          "recordDate": "2017-05-26",
          "time": 3,
          "type": "add"
        },
        {
          "date": "2017-05-31",
          "recordDate": "2017-06-01",
          "time": 1,
          "type": "add"
        },
        {
          "date": "2017-03-31",
          "recordDate": "2017-06-02",
          "time": 40,
          "type": "add"
        },
        {
          "date": "2017-06-02",
          "recordDate": "2017-06-02",
          "time": 3,
          "type": "add"
        },
        {
          "date": "2017-06-05",
          "recordDate": "2017-06-06",
          "time": 3,
          "type": "add"
        },
        {
          "date": "2017-06-09",
          "recordDate": "2017-06-09",
          "time": 4.5,
          "type": "add"
        },
        {
          "date": "2017-06-12",
          "recordDate": "2017-06-14",
          "time": 1.5,
          "type": "add"
        },
        {
          "date": "2017-06-14",
          "recordDate": "2017-06-16",
          "time": 1,
          "type": "minus"
        },
        {
          "date": "2017-06-15",
          "recordDate": "2017-06-16",
          "time": 4,
          "type": "minus"
        },
        {
          "date": "2017-06-17",
          "recordDate": "2017-06-17",
          "time": 2,
          "type": "add"
        },
        {
          "date": "2017-06-19",
          "recordDate": "2017-06-20",
          "time": 1,
          "type": "add"
        },
        {
          "date": "2017-06-20",
          "recordDate": "2017-06-20",
          "time": 1.5,
          "type": "add"
        },
        {
          "date": "2017-06-22",
          "recordDate": "2017-06-23",
          "time": 1.5,
          "type": "add"
        },
        {
          "date": "2017-06-23",
          "recordDate": "2017-06-23",
          "time": 1,
          "type": "add"
        },
        {
          "date": "2017-06-26",
          "recordDate": "2017-06-27",
          "time": 1,
          "type": "add"
        },
        {
          "date": "2017-06-29",
          "recordDate": "2017-06-29",
          "time": 0.5,
          "type": "add"
        },
        {
          "date": "2017-06-30",
          "recordDate": "2017-06-29",
          "time": 8,
          "type": "minus"
        },
        {
          "date": "2017-07-03",
          "recordDate": "2017-07-03",
          "time": 1.5,
          "type": "add"
        },
        {
          "date": "2017-07-04",
          "recordDate": "2017-07-04",
          "time": 1,
          "type": "add"
        },
        {
          "date": "2017-07-05",
          "recordDate": "2017-07-07",
          "time": 2,
          "type": "add"
        },
        {
          "date": "2017-07-07",
          "recordDate": "2017-07-08",
          "time": 3,
          "type": "add"
        },
        {
          "date": "2017-07-17",
          "recordDate": "2017-07-17",
          "time": 2,
          "type": "add"
        },
        {
          "date": "2017-07-18",
          "recordDate": "2017-07-19",
          "time": 1.5,
          "type": "add"
        },
        {
          "date": "2017-07-20",
          "recordDate": "2017-07-20",
          "time": 2,
          "type": "add"
        },
        {
          "date": "2017-07-24",
          "recordDate": "2017-07-26",
          "time": 3.5,
          "type": "add"
        },
        {
          "date": "2017-07-25",
          "recordDate": "2017-07-26",
          "time": 1,
          "type": "add"
        },
        {
          "date": "2017-07-27",
          "recordDate": "2017-07-26",
          "time": 4,
          "type": "minus"
        },
        {
          "date": "2017-07-29",
          "recordDate": "2017-07-30",
          "time": 10,
          "type": "add"
        },
        {
          "date": "2017-07-31",
          "recordDate": "2017-08-01",
          "time": 1,
          "type": "add"
        },
        {
          "date": "2017-08-01",
          "recordDate": "2017-08-01",
          "time": 3,
          "type": "add"
        }
      ].reverse();
      AsyncStorage.setItem('list', JSON.stringify(totalList), () => {
        AsyncStorage.setItem('total', '116.50', () => {
          callback && callback();
        })
      })
    } else {
      callback && callback();
    }
  })
}

export function getTotal(callback) {
  AsyncStorage.getItem('total', (error, total) => {
    callback && callback(parseFloat(total));
  })
}

export function getOvertimeList(callback) {
  AsyncStorage.getItem('list', (error, list) => {
    callback && callback(JSON.parse(list || '[]'));
  })
}

export function addOvertime(data, total, callback) {
  getOvertimeList(list => {
    const totalNum = data.type === 'add' ? (parseFloat(total) + data.time).toFixed(2) : (total - data.time).toFixed(2);
    const totalList = [{...data, recordDate: new Date().format()}, ...list];
    AsyncStorage.setItem('list', JSON.stringify(totalList), () => {
      AsyncStorage.setItem('total', totalNum + '', () => {
        callback && callback(totalNum);
      })
    })
  })
}

export function deleteOvertime(data, total, callback) {
  getOvertimeList(list => {
    const totalNum = data.type === 'minus' ? (parseFloat(total) + data.time).toFixed(2) : (total - data.time).toFixed(2);
    const overtimeList = [...list.slice(0, data.index), ...list.slice(data.index + 1)];
    AsyncStorage.setItem('list', JSON.stringify(overtimeList), () => {
      AsyncStorage.setItem('total', totalNum + '', () => {
        callback && callback({overtimeList, totalNum});
      })
    })
  })
}
